package com.supreme.studyserver.strategy;

import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class StrategyFactory {

    @Resource
    private List<StrategyService> strategyServiceList;

    private Map<String, StrategyService> strategyServiceMap = Collections.emptyMap();

    @PostConstruct
    public void initStrategyHashMap() {
        System.out.println("strategyServiceList = " + strategyServiceList);
        strategyServiceMap = strategyServiceList.stream().collect(
                Collectors.toMap(StrategyService::getStrategyCode, Function.identity())
        );
    }

    public StrategyService getStrategy(String strategyCode) {
        return strategyServiceMap.get(strategyCode);
    }
}
